import launch
import launch_ros
from ament_index_python.packages import get_package_share_directory #获取功能包的资源路径
import os
from launch.substitutions import FindExecutable, PathJoinSubstitution

import launch_ros.parameter_descriptions

def generate_launch_description():   #这个函数名是固定的，不可修改

    #获取自动巡航功能包的路径
    autopartol_robot_path = get_package_share_directory('autopartol_robot')

    action_partol_node = launch_ros.actions.Node(
        package='autopartol_robot',
        executable='partol_robot',
        output='screen',
        parameters=[os.path.join(autopartol_robot_path, 'config', 'partol_config.yaml')],
    )

    action_speaker_node = launch_ros.actions.Node(
        package='autopartol_robot',
        executable='speaker',
        output='screen',
    )

    # #调用其他launch，不能使用
    # gazebo_launch_path = os.path.join(
    #     get_package_share_directory('fishbot_description'),
    #     'launch',
    #     'gazebo_sim.launch.py'
    # )
    # action_gazebo_launch = launch.actions.IncludeLaunchDescription(launch.launch_description_sources.PythonLaunchDescriptionSource(gazebo_launch_path))

    # nav2_launch_path = os.path.join(
    #     get_package_share_directory('fishbot_navigation2'),
    #     'launch',
    #     'navigation2.launch.py'
    # )
    # action_nav2_launch = launch.actions.IncludeLaunchDescription(launch.launch_description_sources.PythonLaunchDescriptionSource(nav2_launch_path))

    #用命令行调用其他launch，可以用，但不能很好的杀死进程，影响下次启动
    #动作3：执行一个命令行
    # action_gazebo = launch.actions.ExecuteProcess(
    # cmd=['ros2','launch','fishbot_description','gazebo_sim.launch.py']
    # )
    # action_nav2 = launch.actions.ExecuteProcess(
    # cmd=['ros2','launch','fishbot_navigation2','navigation2.launch.py']
    # )

    #把多个动作放到一组执行
    action_groupe = launch.actions.GroupAction([
    # launch.actions.TimerAction(period=1.0,actions=[action_gazebo_launch]),
    # launch.actions.TimerAction(period=20.0,actions=[action_nav2_launch]),
    launch.actions.TimerAction(period=1.0,actions=[action_partol_node]),
    launch.actions.TimerAction(period=2.0,actions=[action_speaker_node]),
    ])

    return launch.LaunchDescription([
        #actions动作
        action_groupe,
    ])